import request from "@/utils/request";
import { API_ENDPOINTS } from "@/config/api";

// AI角色接口
export interface AiCharacter {
  id: number;
  name: string;
  personality_type: string;
  description: string;
  difficulty_level: number;
  difficulty_text: string;
  avatar_url: string;
  tags: string[];
  is_favorited?: boolean;
  rating?: number;
  total_sessions?: number;
  interaction_count?: number;
  available_scenarios?: number;
  created_at?: string;
  updated_at?: string;
}

// 训练场景接口
export interface TrainingScenario {
  id: number;
  name?: string;
  title?: string;
  description: string;
  objective?: string;
  difficulty_level: number;
  difficulty?: string;
  difficulty_text?: string;
  tags?: string[];
  success_criteria?: Record<string, any>;
  category?: string;
  estimated_duration?: number;
  sample_conversation?: string;
  rating?: number;
  completion_count?: number;
  created_at?: string;
  updated_at?: string;
  // UI 相关属性
  thumbnail?: string;
  is_popular?: boolean;
  is_favorited?: boolean;
  objectives?: string[];
  skills?: string[];
  total_sessions?: number;
}

// 训练会话接口
export interface TrainingSession {
  id: number;
  user_id: number;
  ai_character_id: number;
  training_scenario_id: number;
  status: "active" | "completed" | "abandoned";
  performance_score?: number;
  feedback?: string;
  created_at: string;
  completed_at?: string;
  character: AiCharacter;
  scenario: TrainingScenario;
}

// 对话消息接口
export interface ConversationMessage {
  id: number;
  content: string;
  sender_type: "user" | "ai";
  emotion_score?: number;
  created_at: string;
}

// 训练消息接口（用于新的训练页面）
export interface TrainingMessage {
  id: number;
  session_id: string;
  sender_type: "user" | "ai";
  content: string;
  feedback?: "helpful" | "not_helpful";
  created_at: string;
}

// 创建训练会话参数
export interface CreateSessionParams {
  ai_character_id: number;
  training_scenario_id: number;
}

// 发送消息参数
export interface SendMessageParams {
  message: string;
}

// 发送消息响应
export interface SendMessageResponse {
  user_message: ConversationMessage;
  ai_response: ConversationMessage;
}

// 训练相关API
export const trainingApi = {
  // 获取AI角色列表
  getCharacters(params?: {
    difficulty_level?: number;
    personality_type?: string;
  }) {
    return request.get<AiCharacter[]>(API_ENDPOINTS.TRAINING.CHARACTERS, {
      params,
    });
  },

  // 收藏AI角色
  favoriteCharacter(characterId: number) {
    return request.post(API_ENDPOINTS.TRAINING.FAVORITE_CHARACTER(characterId));
  },

  // 取消收藏AI角色
  unfavoriteCharacter(characterId: number) {
    return request.delete(
      API_ENDPOINTS.TRAINING.FAVORITE_CHARACTER(characterId)
    );
  },

  // 获取训练场景列表
  getScenarios(params?: { difficulty_level?: number; tags?: string }) {
    return request.get<TrainingScenario[]>(API_ENDPOINTS.TRAINING.SCENARIOS, {
      params,
    });
  },

  // 创建训练会话
  createSession(params: CreateSessionParams) {
    return request.post<TrainingSession>(
      API_ENDPOINTS.TRAINING.SESSIONS,
      params
    );
  },

  // 获取训练会话详情
  getSessionDetail(sessionId: number) {
    return request.get<TrainingSession>(
      API_ENDPOINTS.TRAINING.SESSION_DETAIL(sessionId)
    );
  },

  // 发送消息
  sendMessage(sessionId: number, params: SendMessageParams) {
    return request.post<SendMessageResponse>(
      API_ENDPOINTS.TRAINING.SEND_MESSAGE(sessionId),
      params
    );
  },

  // 完成训练会话
  completeSession(sessionId: number) {
    return request.post(API_ENDPOINTS.TRAINING.COMPLETE_SESSION(sessionId));
  },

  // 放弃训练会话
  abandonSession(sessionId: number) {
    return request.delete(API_ENDPOINTS.TRAINING.SESSION_DETAIL(sessionId));
  },

  // 获取训练复盘
  getSessionReview(sessionId: number) {
    return request.get(API_ENDPOINTS.TRAINING.SESSION_REVIEW(sessionId));
  },

  // 重新开始类似训练
  replaySession(sessionId: number) {
    return request.post<TrainingSession>(
      API_ENDPOINTS.TRAINING.SESSION_REPLAY(sessionId)
    );
  },

  // 获取推荐训练
  getRecommendedTrainings(assessmentId: string) {
    return request.get<TrainingScenario[]>(
      `/assessment/results/${assessmentId}/recommended-trainings`
    );
  },

  // 根据能力类型获取训练
  getTrainingsByAbility(abilityType: string) {
    return request.get<TrainingScenario[]>(
      `/training/scenarios/by-ability/${abilityType}`
    );
  },
};
